package com.son.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.son.hotel.common.ErrorCode;
import com.son.hotel.domain.Customers;
import com.son.hotel.execption.BusinessException;
import com.son.hotel.response.CustomerAndRoomInfoResp;
import com.son.hotel.service.CustomersService;
import com.son.hotel.mapper.CustomersMapper;
import com.son.hotel.vo.CustomAndRoomInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 孙一卓
* @description 针对表【customers(顾客)】的数据库操作Service实现
* @createDate 2025-06-24 14:49:17
*/
@Slf4j
@Service
public class CustomersServiceImpl extends ServiceImpl<CustomersMapper, Customers>
    implements CustomersService{

    @Autowired
    private CustomersMapper customersMapper;

    @Override
    public int addCustom(CustomAndRoomInfo customAndRoomInfo) {
        Customers customers = new Customers();
        customers.setName(customAndRoomInfo.getName());
        customers.setPhone(customAndRoomInfo.getPhone());
        customers.setIdentity(customAndRoomInfo.getIdentity());
        log.info("用户信息customers:{}",customers);
        //在查询之前应该先看看customer表中有没有数据，避免重复插入
        QueryWrapper<Customers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity",customAndRoomInfo.getIdentity());
        Customers cus = customersMapper.selectOne(queryWrapper);
        if (!BeanUtil.isEmpty(cus)){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"该用户已经存在");
        }
        int insert = customersMapper.insert(customers);
        if (insert <= 0){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"插入失败");
        }
        return insert;
    }

    @Override
    public int addCustom(CustomerAndRoomInfoResp customerAndRoomInfoResp) {
        Customers customers = new Customers();
        customers.setName(customerAndRoomInfoResp.getName());
        customers.setPhone(customerAndRoomInfoResp.getPhone());
        customers.setIdentity(customerAndRoomInfoResp.getIdentity());
        log.info("用户信息customers:{}",customers);
        //在查询之前应该先看看customer表中有没有数据，避免重复插入
        QueryWrapper<Customers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity",customerAndRoomInfoResp.getIdentity());
        Customers cus = customersMapper.selectOne(queryWrapper);
        if (!BeanUtil.isEmpty(cus)){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"该用户已经存在");
        }
        int insert = customersMapper.insert(customers);
        if (insert <= 0){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"插入失败");
        }
        return insert;
    }

    /**
     * 根据身份证获取用户信息
     * @return
     */
    public Customers getCustomerByIdentity(String identity){
        QueryWrapper<Customers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity",identity);
        return customersMapper.selectOne(queryWrapper);
    }



}




